<template>
  <div class="h-100">
    <biz-list ref="budinessList" page-name="manager" @operate="operate" />
    <!-- 通道列表 -->
    <segment-detail
      :visible="showSegment"
      :row-data="rowData"
      @close="close"
      @reload="reload"
      @showTestResult="showTestResult"
    />
    <!-- 编辑基本信息 -->
    <edit :visible="showEdit" :row-data="rowData" @close="close" />
    <!-- 智能开通弹窗 -->
    <open :visible="showOpen" :row-data="rowData" @close="close" />
    <!-- 智能恢复 -->
    <restore :visible="showRestore" :row-data="rowData" @close="close" />
    <!-- 智能还原 -->
    <reduction :visible="showReduction" :row-data="rowData" @close="close" />
    <!-- 故障定位 -->
    <fault-location
      :visible="showLocation"
      :location-data="locationData"
      :row-data="rowData"
      @close="close"
    />
    <test-result
      :visible.sync="showResult"
      :row-data="rowData"
      @showTestChannel="showTestChannel"
    />
    <test-params
      :visible.sync="showParams"
      type-name="bizList"
      :cur-otdr-param="{}"
      @saveParam="saveParam"
    />
    <line-detail :row-data="rowData" :visible="lineDetailShow" @close="lineDetailShow = false" />
    <log-detail
      :task-data="rowData"
      :visible.sync="taskLogVisible"
      @close="taskLogVisible = false"
    />
  </div>
</template>

<script>
  import faultLocationApi from '@/api/oen/FaultLocationApi'
  import bizChannelApi from '@/api/oen/BizChannelApi'
  import bizOpenApi from '@/api/oen/BizOpenApi'
  import taskGroupApi from '@/api/oen/TaskGroup'
  import Edit from './Edit'
  import Restore from './Restore'
  import Reduction from './Reduction'
  import SegmentDetail from './SegmentDetail'
  import Open from './Open'
  import BizList from './components/BizList'
  import FaultLocation from './FaultLocation'
  import TestParams from './components/otdrTest/TestParams'
  import TestResult from './components/otdrTest/TestResult'
  import LineDetail from '@/views/module/link/record/components/LineDetail'
  import LogDetail from '@/views/module/info/process/components/LogDetail'

  export default {
    name: 'BusinessManagerPage',
    components: {
      BizList,
      Edit,
      SegmentDetail,
      Open,
      Restore,
      Reduction,
      FaultLocation,
      TestParams,
      TestResult,
      LineDetail,
      LogDetail
    },
    data() {
      return {
        showRestore: false,
        showOpen: false,
        showEdit: false,
        showSegment: false,
        showReduction: false,
        showLocation: false,
        showParams: false,
        showResult: false,
        lineDetailShow: false,
        rowData: {},
        locationData: {},
        taskLogVisible: false,
        functionName: 'manager'
      }
    },
    provide() {
      return {
        functionName: 'manager',
        parentVM: this // 父组件实例
      }
    },
    methods: {
      // 操作
      operate(data) {
        this.rowData = data.data
        switch (data.type) {
        case 'channel':
          // 修改通道
          this.showSegment = true
          break
        case 'mod':
          // 修改基础信息
          this.showEdit = true
          break
        case 'restore':
          //   智能恢复
          this.rowData.functionName = 'manager'
          this.rowData.operatorType = 'restore'
          this.showRestore = true
          break
        case 'reduction':
          //   智能还原
          this.showReduction = true
          break
        case 'add':
          //   智能开通
          this.rowData = {}
          this.showOpen = true
          break
        case 'reopening':
          //   重新开通
          this.showOpen = true
          break
        case 'faultLocation':
          //   故障定位
          this.openLocation()
          break
        case 'otdrTest':
          this.otTest(data.data)
          break
        case 'otdrTestResult':
          this.showResult = true
          break
        default:
          break
        }
      },
      openLocation() {
        faultLocationApi.ready({ bizId: this.rowData.bizId }).then(res => {
          this.locationData = res
          this.showLocation = true
        })
      },
      otTest(row) {
        this.$confirm(this.$t('OTDR_TEST_CONFIRM')).then(() => {
          bizChannelApi.getDetailByBizId({ bizId: row.bizId }).then(res => {
            this.rowData.bizChannelList = res
            this.showParams = true
          })
        })
      },
      async saveParam(data) {
        // console.log('saveParam', data)
        // console.log('rowData', this.rowData)
        const portList = []
        this.rowData.bizChannelList.forEach(i => {
          const linkData = i.segmentList.map((j, index) => {
            const item = j
            item.startNode.afsStatus = null
            item.endNode.afsStatus = null
            return {
              networkSegment: null,
              segmentChannel: item
            }
          })
          const startPortList = []
          const endPortList = []
          const cableIds = []
          i.segmentList.forEach(j => {
            if (j.startPortCategory === 'VARIABLE_POINT') {
              startPortList.push({
                portId: j.startPortId,
                deviceId: j.startNodeId,
                testDirection: 'Z',
                networkLinkChannelBoList: linkData
              })
            }
            if (j.endPortCategory === 'VARIABLE_POINT') {
              endPortList.push({
                portId: j.endPortId,
                deviceId: j.endNodeId,
                testDirection: 'A',
                networkLinkChannelBoList: linkData
              })
            }
            j.fiber && cableIds.push(j.fiber.cableId)
          })
          let port = null
          let connPort = null
          if (data.direction === 'A') {
            port = endPortList.at(-1)
            connPort = startPortList.at(0)
          }
          if (data.direction === 'Z') {
            port = startPortList.at(0)
            connPort = endPortList.at(-1)
          }
          portList.push({
            ...port,
            cableIds: cableIds,
            connPortId: connPort.portId,
            connDeviceId: connPort.deviceId
          })
        })
        const params = {
          bizId: this.rowData.bizId,
          processName: data.processName,
          linkSegmentId: null,
          otdrParam: data,
          portList: portList,
          bizName: this.rowData.bizName,
          functionName: 'manager'
        }
        console.log('params', params)
        const connPort = await bizOpenApi.getOpposite({
          portId: portList[0].portId,
          cut: false
        })
        if (connPort.portCategory !== 'VARIABLE_POINT') {
          this.$message.error(
            this.$t('CANNOT_THROUGH_TEST', {
              direction: this.$t(data.direction + '_END'),
              side: data.direction
            })
          )
          return
        }
        bizOpenApi.throughTest(params).then(res => {
          this.$message.success(this.$t('SAVE_SUCCESS'))
          this.showParams = false
          this.$router.push({
            name: 'MENU_PROCESS_LIST',
            params: {
              processId: res.processId,
              taskId: res.taskId
            }
          })
        })
      },
      showTestChannel(data) {
        let testDirection = ''
        this.rowData.bizChannelList = data.portList.map(i => {
          testDirection = i.testDirection
          return {
            segmentList: i.networkLinkChannelBoList.map(j => j.segmentChannel)
          }
        })
        this.rowData.isBizOtdrResult = true
        this.rowData.bizType = 'BIZ_THROUGH'
        this.rowData.testTime = data.testTime
        this.rowData.processId = data.processId
        this.rowData.taskIdList = data.taskId
        this.rowData.testDirection = testDirection
        this.showSegment = true
      },
      showTestResult(data) {
        this.rowData = {
          ...this.rowData,
          ...data,
          isBizOtdrResult: true
        }
        if (!data.processId) {
          this.lineDetailShow = true
          return
        }
        taskGroupApi.getByProcessId({ processId: data.processId }).then(res => {
          if (res.status === 'SUCCESS') {
            this.lineDetailShow = true
          } else if (res.status === 'READY' || res.status === 'PROCESS') {
            this.showResult = false
            // this.showSegment = false
            this.$router.push({
              name: 'MENU_PROCESS_LIST',
              params: {
                processId: data.processId,
                taskId: [data.taskId],
                needBack: true,
                backUrl: 'MENU_BUSINESS_MANAGER'
              }
            })
          } else {
            res.taskList.forEach(i => {
              i.subTaskList.forEach(j => {
                if (j.subTaskId === data.taskId) {
                  this.rowData = {
                    ...this.rowData,
                    ...j
                  }
                  if (j.status !== 'SUCCESS') {
                    this.taskLogVisible = true
                  } else {
                    this.lineDetailShow = true
                  }
                }
              })
            })
          }
        })
      },
      close(load) {
        this.showReduction = false
        this.showRestore = false
        this.showEdit = false
        this.showSegment = false
        this.showOpen = false
        this.showLocation = false
        if (load === true) {
          this.$refs.budinessList && this.$refs.budinessList.loadData()
        }
      },
      reload() {
        this.$refs.budinessList && this.$refs.budinessList.loadData()
      }
    }
  }
</script>

<style></style>
